package cn.tedu._03vehicle.base.exception;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(IllegalArgumentException.class)
    public JsonResult doHandleIllegalArgumentException(IllegalArgumentException ex) {
       String data=ex.getMessage();
       log.error("服务器处理请求时出现异常，异常信息：{}",data);
        return JsonResult.fail(data);
    }
   @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult doHandleRuntimeException(ConstraintViolationException ex) {
        String data=ex.getMessage().split(":")[1].trim();
        log.error("服务器处理请求时出现异常，异常信息：{}",data);
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult doHandleMethodArgumentNotValidException
            (MethodArgumentNotValidException ex) {
        String data=ex.getFieldError().getDefaultMessage();
        log.error("服务器处理请求时出现异常，异常信息：{}",data);
        return new JsonResult(StatusCode.OPERATTION_FAILED,data);
    }
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public JsonResult doHandleMaxUploadSizeExceededException
            (MaxUploadSizeExceededException ex) {
        String data = "上传文件大小超出限制，单个文件不超过100MB,单次多个文件不超过500MB";
        log.error("doHandleMaxUploadSizeExceededException: " + data);

        return new JsonResult(StatusCode.MAX_UPLOAD_SIZE_EXCEEDED,data);
    }
    @ExceptionHandler(RuntimeException.class)
    public JsonResult doHandleRuntimeException(RuntimeException ex){
        String data = ex.getMessage();
        log.error("doHandleRuntimeException: " + data);

        return JsonResult.fail(data);
    }
    /* @ExceptionHandler(Throwable.class)
    public JsonResult doHandleThrowable(Throwable ex) {
        String data=ex.getMessage();
        log.error("服务器处理请求时出现异常，异常信息：{}",data);
        return new JsonResult(StatusCode.OPERATTION_FAILED,data);
    }*/
}
